package com.chunxuan.langquan.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.GsonUtils;
import com.chunxuan.langquan.R;
import com.chunxuan.langquan.dao.bean.ReceiptAddressInfo;
import com.chunxuan.langquan.ui.activity.AddressManageActivity;
import com.chunxuan.langquan.ui.activity.EditAddressActivity;

import java.util.List;

public class AddressListAdapter extends RecyclerView.Adapter {
    private List<ReceiptAddressInfo> receiptAddressInfoList;
    private Context context;

    public AddressListAdapter(Context context, List<ReceiptAddressInfo> receiptAddressInfoList) {
        this.context = context;
        this.receiptAddressInfoList = receiptAddressInfoList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item_address, parent, false);
        final AddressViewHolder addressViewHolder = new AddressViewHolder(v);
        addressViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ReceiptAddressInfo addressInfo = receiptAddressInfoList.get(addressViewHolder.getAdapterPosition());
                Intent intent = new Intent();
                intent.putExtra("address", GsonUtils.toJson(addressInfo));
                ((AddressManageActivity) parent.getContext()).setResult(200, intent);
                ((AddressManageActivity) parent.getContext()).finish();
            }
        });
        return addressViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        AddressViewHolder addressViewHolder = (AddressViewHolder) holder;
        final ReceiptAddressInfo receiptAddressInfo = receiptAddressInfoList.get(position);
        if (null != receiptAddressInfo) {
            addressViewHolder.tvName.setText(receiptAddressInfo.getName());
            addressViewHolder.tvMobile.setText(receiptAddressInfo.getMobile());
            addressViewHolder.tvAddress.setText(receiptAddressInfo.getProvince_name() + receiptAddressInfo.getCity_name()
                    + receiptAddressInfo.getArea_name() + receiptAddressInfo.getAddress());
        }
        addressViewHolder.tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("receiptAddressInfo", receiptAddressInfo);
                Intent intent = new Intent(context, EditAddressActivity.class);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return receiptAddressInfoList == null ? 0 : receiptAddressInfoList.size();
    }

    static class AddressViewHolder extends RecyclerView.ViewHolder {

        TextView tvName;
        TextView tvMobile;
        TextView tvAddress;
        TextView tvEdit;

        public AddressViewHolder(@NonNull View itemView) {
            super(itemView);

            tvName = itemView.findViewById(R.id.tv_name);
            tvMobile = itemView.findViewById(R.id.tv_mobile);
            tvAddress = itemView.findViewById(R.id.tv_address);
            tvEdit = itemView.findViewById(R.id.tv_edit);
        }
    }
}
